Find Perimeter of a Trapezoid

Theory:

The perimeter of a trapezoid is calculated by adding the lengths of all its sides. For a trapezoid with non-parallel sides a, b, and parallel sides c and d, the perimeter is given by a + b + c + d.

Python Code:

def calculate_trapezoid_perimeter(side1, side2, base1, base2):
    return side1 + side2 + base1 + base2

# Taking input for the lengths of the sides and bases of the trapezoid and calculating its perimeter
def calculate_and_display_trapezoid_perimeter():
    side1 = float(input("Enter the length of side 1: "))
    side2 = float(input("Enter the length of side 2: "))
    base1 = float(input("Enter the length of base 1: "))
    base2 = float(input("Enter the length of base 2: "))
    perimeter = calculate_trapezoid_perimeter(side1, side2, base1, base2)
    print("Perimeter of the trapezoid:", perimeter)

calculate_and_display_trapezoid_perimeter()

Example Output 1:

Enter the length of side 1: 5

Enter the length of side 2: 7

Enter the length of base 1: 4

Enter the length of base 2: 6

Perimeter of the trapezoid: 22.0

Example Output 2:

Enter the length of side 1: 8.5

Enter the length of side 2: 6.2

Enter the length of base 1: 7

Enter the length of base 2: 9

Perimeter of the trapezoid: 30.7

Code Explanation:

The function calculate_trapezoid_perimeter(side1, side2, base1, base2) calculates the perimeter of a trapezoid by adding the lengths of all its sides.

The function calculate_and_display_trapezoid_perimeter() takes input for the lengths of the sides and bases of the trapezoid, calculates its perimeter using the aforementioned function, and displays the result.